﻿#pragma once
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/web/mime/multipart/Multipart.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "../dto/SimpleDto.h"
#include "../util/TextUtil.h"
#include "../util/SystemHelper.h"

#include OATPP_CODEGEN_BEGIN(ApiController)

/**
 * 系统管理控制器
 */
class SystemController : public oatpp::web::server::api::ApiController {
public:
	SystemController(const std::shared_ptr<ObjectMapper>& objectMapper)
	: oatpp::web::server::api::ApiController(objectMapper) 
	{}

	/**
	 * 重启系统
	 */
	ENDPOINT("GET", "/system/reboot", rebootSystem,
		HEADER(String, authorization, "Authorization")) {

		// 返回操作结果
		auto result = SimpleDto::createShared();
		result->error = -1;
		result->description = "服务器内部错误!";

		// 查找用户信息
		if (SystemConfig::get_mutable_instance().GetBoxConf().GetUser().GetToken()
			!= TextUtil::get_mutable_instance().GetToken(authorization.getValue("")))
		{
			result->description = "无效令牌!";
			return createDtoResponse(Status::CODE_200, result);
		}

		// 重启气筒
		SystemHelper::get_mutable_instance().Reboot();

		// 返回登录结果
		result->error = 0;
		result->description = "success";
		return createDtoResponse(Status::CODE_200, result);
	}
};

#include OATPP_CODEGEN_END(ApiController)